#coding=utf-8
import random
from PIL import Image,ImageDraw, ImageFont, ImageFilter

font_path=r'C:\Windows\Fonts\Arial.ttf'

def randChar(): return chr(random.randint(65,90))  #随机生成4个字母  48-57数字，65-90字母
def randRGB1():   #随机颜色1
    return (random.randint(64,255),random.randint(64, 255),random.randint(64, 255))
def randRGB2():   #随机颜色2
    return (random.randint(32,127),random.randint(32, 127),random.randint(32, 127)) 

def gene_code():             #生成验证码
    width,height=(60*4,60)       #宽和高
    image=Image.new('RGB',(width,height),(255,255,255))  #创建图片
    font=ImageFont.truetype(font_path,36)     #创建font对象
    draw=ImageDraw.Draw(image)      #创建draw对象
    #for x,y in zip(range(width),range(height)):
        #draw.point((x,y),fill=randRGB1())
    for x in range(width):
        for y in range(height):
            draw.point((x,y),fill=randRGB1())      #填充每个像素
    for t in range(4):
        draw.text((60*t+10,10),randChar(),font=font,fill=randRGB2())   #输出文字
    image=image.filter(ImageFilter.BLUR)    #模糊
    #image=image.filter(ImageFilter.EDGE_ENHANCE_MORE) #强化边缘
    image.save('idencode3.png')     #保存验证码图片

if __name__=="__main__": #随机字符
    gene_code()